<?php
namespace app\module\controllers;

use app\models\BlockRecorder;
use app\models\CateRecorder;
use app\models\SetRecorder;
use yii\web\Controller;

class BaseController extends Controller{
    public $enableCsrfValidation=false;

    public function beforeAction($action)
    {
        $set = SetRecorder::find()->where('set_id=1')->one();
        $set_info = $set->set_info;
        $set_info = json_decode($set_info,true);
        \Yii::$app->params = array_merge(\Yii::$app->params,$set_info);
        return parent::beforeAction($action); // TODO: Change the autogenerated stub
    }

    public function get_block($mb_id=0){
        if(empty($mb_id)){
            return ['result'=>1,'data'=>[]];
        }
        $model = BlockRecorder::find()->select(['id','title','content','pics','thumb_pic','mb_id'])->where('mb_id='.$mb_id)->asArray()->one();
        if($model){
            $model['pics'] = json_decode($model['pics']);
            $arr = [];
            foreach($model['pics'] as $v){
                $arr[]=str_replace('../',\Yii::$app->params['pic_url'],$v);
            }
            $model['pics'] = $arr;
            $model['thumb_pic'] = str_replace("../",\Yii::$app->params['pic_url'],$model['thumb_pic']);
            $result = ['result'=>1,'data'=>$model];
        }else{
            $result = ['result'=>1,'data'=>[]];
        }
        return $result;
    }

    public function get_child_cate($parent=0,$arr=[]){
        $list = CateRecorder::find()->select(['id'])->where('parent_id='.$parent)->asArray()->all();
        foreach($list as $k=>$v){
            $arr[]=$v['id'];
            $arr=$this->get_child_cate($v['id'],$arr);
        }
        return $arr;
    }

    public function get_cate_list($parent_id=0,$level=0,$str="--",$arr=[]){
        $list = CateRecorder::find()->select(['id','cate_name','thumb_pic'])->where('parent_id=:id',[':id' => $parent_id])->orderBy('cate_order ASC')->asArray()->all();
        foreach($list as $k=>$v){
            $level = (!$parent_id)?0:++$level;
            $list[$k]['str']=(!$parent_id)?"":"|".str_repeat($str,$level);
            $list[$k]['thumb_pic'] = empty($v['thumb_pic'])?'':str_replace("../",\Yii::$app->params['pic_url'],$v['thumb_pic']);
            $arr[]=$list[$k];
            $arr=$this->get_cate_list($v['id'],$level,$str,$arr);
        }
        return $arr;
    }

}
